<?php
	/**
	* @package ParkFind
	* @desc This is the proposal receipt page. This is tells user whether proposal was successfully submitted or not.
	*/
	
	// Direct calling check
	if (!isset($_POST["details"]))
		die("Do not call this page directly");
		
	session_start();

	/**
	* @desc This include file connects to the database.
	*/
	require 'includes/connect.inc';
	
	// Store variables, escape them aswell
	$type = mysql_real_escape_string($_POST["type"], $connection);
	$address =  mysql_real_escape_string($_POST["address"], $connection);
	$suburb_id = mysql_real_escape_string($_POST["suburb"], $connection);
	$sugname = mysql_real_escape_string($_POST["name"], $connection);
	$details = mysql_real_escape_string($_POST["details"], $connection);

	$address = htmlentities($address);
	$sugname = htmlentities($sugname);
	$details = htmlentities($details);
		
	// Make arrays
	$_SESSION["errors"] = array();
	$_SESSION["form"] = array();
	
	// Address validation		
	If (strlen($address) == 0)
		$_SESSION["errors"]["address"] = "Fill in an address";
		
	else	
	{
		$_SESSION["form"]["address"] = $address;
		unset($_SESSION["errors"]["address"]);
	}
	
	// Suburb validation		
	If ($suburb_id == "---")		
		$_SESSION["errors"]["suburb"] = "<<< Oops";
	
	else	
	{
		$_SESSION["form"]["suburb"] = $suburb_id;
		unset($_SESSION["errors"]["suburb"]);
	}
	
	// Type validation		
	If ($type == "---")	
		$_SESSION["errors"]["type"] = "<<< Oops";
		
	else	
	{
		$_SESSION["form"]["type"] = $type;
		unset($_SESSION["errors"]["type"]);
	}
	
	//Suggested Name	
	$_SESSION["form"]["sugname"] = $sugname;
	
	// Details validation		
	If (strlen($details) == 0)		
		$_SESSION["errors"]["details"] = "Fill in details";
	
	else	
	{
		$_SESSION["form"]["details"] = $details;
		unset($_SESSION["errors"]["details"]);
	}
	
		
	// If there were any errors, show the page again
	if (count($_SESSION["errors"]))
	{
		mysql_close($connection); // Close connection
		header("Location: proposal.php");
		exit;
	}

	/**
	* @desc This include file will load the functions that are required to call 
	*		from the database
	*/
	require 'includes/db_functions.inc';
	
	// Construct report
	$comment = "Suggested Name: " . $sugname . "\n" .
				"Type: " . $type . "\n\n" .
				$details;
	
	// Add Proposal
	$result = addProposal($_SESSION["user_id"], $comment, $address, $suburb_id);
	
	if ($result == TRUE)
		$status = "success";
	else 
		$status = "failed";
	
	// Redirect
	header("Location: proposalreceipt.php?result=" . $status);
	
	mysql_close($connection); // Close connection

	
?>
